package com.easy.modules.sysmgr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easy.common.dao.IBaseDao;
import com.easy.common.util.BeanUtils;
import com.easy.common.util.string.UUIDHexGenerator;
import com.easy.common.web.page.DataGrid;
import com.easy.common.web.page.Msg;
import com.easy.common.web.page.Pager;
import com.easy.common.web.servlet.ApplicationConstants;
import com.easy.modules.sysmgr.entity.GlobalEntity;
import com.easy.modules.sysmgr.service.GlobalServiceI;
import com.easy.modules.sysmgr.web.form.GlobalForm;

@Service
@Transactional
public class GlobalServiceImpl implements GlobalServiceI {

	@Autowired
	private IBaseDao<GlobalEntity> basedaoGlobal;
	
	@Override
	public Msg saveORupdate(GlobalForm form) {
		try {
			GlobalEntity entity = this.basedaoGlobal.load(GlobalEntity.class, form.getId()) ;
			
			if(null != entity) {	
				//将旧的信息保存为历史
				GlobalEntity history = new GlobalEntity() ;
				BeanUtils.copyNotNullProperties(entity, history) ;
				history.setId(UUIDHexGenerator.generator()) ;
				this.basedaoGlobal.add(history) ;		
				
				//再修改当前记录
				BeanUtils.copyNotNullProperties(form, entity) ;
				entity.setModifyDate(new Date()) ;
				this.basedaoGlobal.update(entity) ;
				
				BeanUtils.copyNotNullProperties(entity, form) ;
				ApplicationConstants.GLOBAL_MAP.put("global", form) ;
				
				return new Msg(true, "编辑成功！") ;
			}
			
		} catch (BeansException e) {
			return new Msg(false, "发生错误！") ;
		}
		return new Msg(false, "编辑失败！") ;
	}

	@Override
	public Msg delete(GlobalForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					this.basedaoGlobal.delete(GlobalEntity.class, id) ;
				}
				return new Msg(true, "删除成功！") ;
			} else {
				return new Msg(false, "删除失败！") ; 
			}
		} catch (BeansException e) {
			return new Msg(false, "删除失败！") ;
		}
	}
	
	@Override
	public GlobalForm get(GlobalForm form) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from GlobalEntity t where 1=1";
		hql = addWhere(hql, form, params);
		
		GlobalEntity entity = (GlobalEntity) this.basedaoGlobal.queryObject(hql, params) ;
		if(null != entity) {
			GlobalForm pform = new GlobalForm();
			BeanUtils.copyProperties(entity, pform);
			
			return pform;
		} else {
			return null ;
		}
	}

	@Override
	public DataGrid datagrid(GlobalForm form) {
		
		List<GlobalForm> forms = new ArrayList<GlobalForm>();
		Pager<GlobalEntity> page = this.find(form) ;
		if (null != page && !page.getDataRows().isEmpty()) {
			for (GlobalEntity i : page.getDataRows()) {
				GlobalForm uf = new GlobalForm();
				BeanUtils.copyProperties(i, uf);
				
				forms.add(uf);
			}
		}
		
		DataGrid datagrid = new DataGrid();
		datagrid.setTotal(page.getTotal());
		datagrid.setRows(forms);
		return datagrid;
	}


	private Pager<GlobalEntity> find(GlobalForm form) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from GlobalEntity t where 1=1";
		hql = addWhere(hql, form, params) ;
		return this.basedaoGlobal.find(hql, params);
	}

	private String addWhere(String hql, GlobalForm form, Map<String, Object> params) {
		if (form.getId() != null && !"".equals(form.getId())) {
			hql += " and t.id=:id";
			params.put("id", form.getId());
		} else {
			hql += " and t.id=:id";
			params.put("id", "000000");
		}
		return hql;
	}
}
